# Minimum CMake required
cmake_minimum_required(VERSION 3.15)

# Project
project(StockDataRpc)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)

set(project_root C:\\Users\\Y\\Documents\\code\\qt_project\\StockDataRpc
)
set(CMAKE_PREFIX_PATH  
#C:\\Users\\Y\\Documents\\code\\grpc\\build32\\Release
C:\\Users\\Y\\Documents\\code\\grpc\\install\\bin
C:\\Users\\Y\\Documents\\code\\grpc\\install\\lib
C:\\Users\\Y\\Documents\\code\\grpc\\install\\cmake
C:\\Users\\Y\\Documents\\code\\grpc\\install\\lib\\cmake\\grpc
C:\\Users\\Y\\Documents\\code\\qt-6.0.0-build-32
)

set(qt_root C:\\Users\\Y\\Documents\\code\\qt-6.0.0-build-32)
include_directories(
${qt_root}\\include\\QtCore
${qt_root}\\include\\QtSql 
${qt_root}\\include
C:\\Users\\Y\\Documents\\code\\grpc\\install\\include
)
find_package(Qt6 COMPONENTS Core Sql  REQUIRED)

# Protobuf
#set(protobuf_MODULE_COMPATIBLE TRUE)
find_package(Protobuf CONFIG REQUIRED)
message(STATUS "Using protobuf ${protobuf_VERSION}")

# Protobuf-compiler
set(_PROTOBUF_PROTOC $<TARGET_FILE:protobuf::protoc>)

# gRPC
find_package(gRPC CONFIG REQUIRED)
message(STATUS "Using gRPC ${gRPC_VERSION}")
set(_GRPC_GRPCPP gRPC::grpc++)
set(_GRPC_CPP_PLUGIN_EXECUTABLE $<TARGET_FILE:gRPC::grpc_cpp_plugin>)

find_package(Threads REQUIRED)

# Proto file
get_filename_component(hw_proto "dataexchange.proto" ABSOLUTE)
get_filename_component(hw_proto_path "${hw_proto}" PATH)




# Generated sources
set(hw_proto_srcs "${CMAKE_CURRENT_BINARY_DIR}/dataexchange.pb.cc")
set(hw_proto_hdrs "${CMAKE_CURRENT_BINARY_DIR}/dataexchange.pb.h")
set(hw_grpc_srcs "${CMAKE_CURRENT_BINARY_DIR}/dataexchange.grpc.pb.cc")
set(hw_grpc_hdrs "${CMAKE_CURRENT_BINARY_DIR}/dataexchange.grpc.pb.h")

add_custom_command(
      OUTPUT "${hw_proto_srcs}" "${hw_proto_hdrs}" "${hw_grpc_srcs}" "${hw_grpc_hdrs}"
      COMMAND ${_PROTOBUF_PROTOC}
      ARGS --grpc_out "${CMAKE_CURRENT_BINARY_DIR}"
        --cpp_out "${CMAKE_CURRENT_BINARY_DIR}"
        -I "${hw_proto_path}"
        --plugin=protoc-gen-grpc="${_GRPC_CPP_PLUGIN_EXECUTABLE}"
        "${hw_proto}"
      DEPENDS "${hw_proto}")

# Include generated *.pb.h files
include_directories(
C:\\Users\\Y\\Documents\\code\\libevent-2.1.12\\install\\include
${project_root}
"${CMAKE_CURRENT_BINARY_DIR}"
)

#add_definitions(EVENT__SIZEOF_SIZE_T 4)

find_library(LIB_EVENT event_core C:\\Users\\Y\\Documents\\code\\libevent-2.1.12\\install\\lib)
ADD_SUBDIRECTORY(event_loop)
ADD_SUBDIRECTORY(common)

# Targets (client|server)
foreach(_target
  client server)
    add_executable(${_target} 
    ${hw_proto_srcs}
    ${hw_grpc_srcs})
  target_link_libraries(${_target}
    ${_REFLECTION}
    ${_GRPC_GRPCPP}
    ${_PROTOBUF_LIBPROTOBUF}
    Qt6::Core
    Qt6::Sql
    ${LIB_EVENT} 
    "C:\\Program Files (x86)\\Windows Kits\\10\\Lib\\10.0.18362.0\\um\\x86\\Iphlpapi.lib"
event_loop
common
    )
endforeach()

ADD_SUBDIRECTORY(server_side)
ADD_SUBDIRECTORY(client_side)

